﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DevTool.Command
{
    /// <summary>
    /// 文件操作
    /// </summary>
    public class FileHelper
    {
        /// <summary>
        /// 文件转换成Base64字符串
        /// </summary>
        /// <param name="fileName">文件绝对路径</param>
        /// <returns></returns>
        public static String FileToBase64(string fileName)
        {
            string strRet = null;

            try
            {
                using (var fs = new FileStream(fileName, FileMode.Open))
                {
                    byte[] bt = new byte[fs.Length];
                    fs.Read(bt, 0, bt.Length);
                    strRet = Convert.ToBase64String(bt);
                }

            }
            catch (Exception)
            {
                throw;
            }

            return strRet;
        }
        /// <summary>
        /// 文件读取
        /// </summary>
        /// <param name="root">路径</param>
        /// <returns></returns>
        public static string ReadFile(string root, Encoding encoding)
        {
            var result = string.Empty;
            if (File.Exists(root))
            {
                try
                {
                    result = File.ReadAllText(root, encoding);
                }
                catch (Exception)
                {

                    throw;
                }

            }
            return result;

        }
        /// <summary>
        /// 文件写入
        /// </summary>
        /// <param name="root">文件路径</param>
        /// <param name="text">内容</param>
        /// <param name="encoding">编码</param>
        public static void WriteFile(string root, string text, Encoding encoding)
        {


            try
            {
                File.WriteAllText(root, text, encoding);

            }
            catch (Exception)
            {

                throw;
            }


        }
    }
}
